﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BHair.xitong;
using System.Configuration;

namespace BHair.xitong
{
    public partial class frm项目转换 : Form
    {
        public frm项目转换()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql =string.Format( "SELECT  [mname] FROM [member] where mid='{0}'",textBox1.Text);
            SqlCommand comm = new SqlCommand(sql,conn);
            SqlDataReader sdr = comm.ExecuteReader();
            while (sdr.Read())
            {
                label3.Text = sdr["mname"].ToString();
            }
            //查询卡内项目
            kn();

        }
        public void kn()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT [jcxmname] FROM [csmx] where cardID='{0}'",textBox1.Text);
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataSet ds = new DataSet();
            sda.Fill(ds,"csmx");
            comboBox1.DataSource = ds.Tables["csmx"];
            comboBox1.DisplayMember = "jcxmname";
            comboBox1.SelectedIndex = -1;
        }
        public void jz()
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "SELECT  [iname] FROM [items]";
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataSet ds = new DataSet();
            sda.Fill(ds,"items");
            comboBox2.DataSource = ds.Tables["items"];
            comboBox2.DisplayMember = "iname";
            comboBox2.SelectedIndex = -1;

        }

        private void frm项目转换_Load(object sender, EventArgs e)
        {
            jz();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //修改
            string kn = comboBox1.Text;
            string up = comboBox2.Text;
            string upid="";
            string bxiugaiid="";
            //查询修改项目编号
            string sql =string.Format( "SELECT [iid] FROM [items] where iname='{0}'",up);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql,conn);
            SqlDataReader sdr = comm.ExecuteReader();
            while (sdr.Read())
            {
                upid = sdr["iid"].ToString();
            }
            conn.Close();
            //查询被修改项目编号
            SqlConnection conn1 = new SqlConnection(constr);
            conn1.Open();
            string sql1 = string.Format("SELECT [iid] FROM [items] where iname='{0}'", kn);
            SqlCommand comm1 = new SqlCommand(sql1,conn1);
            SqlDataReader sdr1 = comm1.ExecuteReader();
            while (sdr1.Read())
            {
                bxiugaiid = sdr1["iid"].ToString();
            }
            //修改卡内项目
            chuanzhi cz = new chuanzhi();
            cz.rizhi("卡号："+textBox1.Text+"项目："+kn+"转换为："+up);
            upk(up,upid,bxiugaiid,kn);
           // uplsxf(up,upid,bxiugaiid,kn);
            this.Close();
        }
        public void uplsxf(string upname, string upiid, string oldiid, string oldname)
        {
            string sql =string.Format( "UPDATE [jcxf] SET [xmname] = '{0}',[xmid] ='{1}' WHERE xmname='{2}' and cardid='{3}' and xmid='{4}'",upname,upiid,oldname,textBox1.Text,oldiid);
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            SqlCommand comm = new SqlCommand(sql,conn);
            comm.ExecuteNonQuery();

        }
        public void upk(string upname,string upiid,string oldiid,string oldname )
        {
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql =string.Format( "UPDATE [csmx]SET  [jcxmname] = '{0}' ,[jcspid] ='{1}' WHERE cardID='{2}' and jcspid='{3}' and jcxmname='{4}'",upname,upiid,textBox1.Text,oldiid,oldname);
            SqlCommand comm = new SqlCommand(sql,conn);
            int row = comm.ExecuteNonQuery();
            if (row > 0)
            {
                MessageBox.Show("修改成功");
                
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }
    }
}
